package com.kaiwukj.android.ufamily.mvp.ui.adapter;

import android.util.SparseBooleanArray;
import android.widget.CheckedTextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.PropertyFeeResult;

import java.util.ArrayList;

/**
 * Copyright © KaiWu Technology Company
 *
 * @author Haife
 * @job Android Development
 * @company KW | 开物科技
 * @time 2019/7/24
 * @desc $desc
 */
public class PayFeeHouseAdapter extends BaseQuickAdapter<PropertyFeeResult, BaseViewHolder> {
    private onCheckedChangedListener mListener;

    private SparseBooleanArray sparseBooleanArray;
    private int lastCheckedPos = -1;

    public PayFeeHouseAdapter() {
        super(R.layout.item_pay_fee_house, new ArrayList<>());
        sparseBooleanArray = new SparseBooleanArray(getItemCount());
    }

    public void check(int pos) {
        if (pos == lastCheckedPos) return;
        if (lastCheckedPos != -1) {
            sparseBooleanArray.put(lastCheckedPos, false);
            notifyItemChanged(lastCheckedPos);
        }
        lastCheckedPos = pos;
        sparseBooleanArray.put(lastCheckedPos, true);
        notifyItemChanged(lastCheckedPos);
        if (mListener != null) mListener.onCheckedChanged(lastCheckedPos);
    }

    @Override
    protected void convert(BaseViewHolder helper, PropertyFeeResult item) {
        helper.setText(R.id.tv_room, item.getReferenceNo() + item.getEstateName());
        CheckedTextView tvRoom = helper.getView(R.id.tv_room);
        tvRoom.setChecked(sparseBooleanArray.get(helper.getAdapterPosition()));
        helper.itemView.setOnClickListener(v -> check(helper.getAdapterPosition()));
    }

    public interface onCheckedChangedListener {
        void onCheckedChanged(int pos);
    }

    public void setOnCheckedChangedListener(onCheckedChangedListener onSelectedListener) {
        this.mListener = onSelectedListener;
    }

}
